package member.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import member.model.MemberService;
import member.model.MemberVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import board.model.BoardService;

public class MemberController extends MultiActionController{
	private MemberService memberService;
	private BoardService boardService;
	private MemberController(MemberService memberService,BoardService boardService) {
		this.memberService = memberService;
		this.boardService=boardService;
	}
	public ModelAndView memberRegist(HttpServletRequest request,HttpServletResponse response,MemberVO memberVO) throws Exception{
		memberService.memberRegist(memberVO);
		return new ModelAndView("registed.member");
	}
	public ModelAndView memberExistId(HttpServletRequest request,HttpServletResponse response){
		boolean flag=false;
		try {
			flag = memberService.memberExistId(request.getParameter("id"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return new ModelAndView("JsonView","memberExistId",flag);
	}
	@SuppressWarnings("deprecation")
	public ModelAndView memberLogin(HttpServletRequest request,HttpServletResponse response,MemberVO memberVO){
		MemberVO memberVOLogedIn = null;
		try {
			memberVOLogedIn = memberService.memberLogin(memberVO);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(memberVOLogedIn!=null){
			request.getSession(false).setAttribute("memberVO",memberVOLogedIn);
			if(memberVOLogedIn.getId().equals("admin")){
				return new ModelAndView("index");
			}
			Date today=new Date();
			Date connectedDate=memberVOLogedIn.getConnectedDate();
			MemberVO matchedVO=null; 
			if(connectedDate!=null){
				if(connectedDate.getYear()==today.getYear()&&connectedDate.getMonth()==today.getMonth()&&connectedDate.getDay()==today.getDay()){
					return new ModelAndView("index");
				}
			}
			memberVOLogedIn.setConnectedDate(today);
			try {
				memberService.memberUpdate(memberVOLogedIn);
				matchedVO=memberService.getMatchedVO(memberVO.getId());
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return new ModelAndView("index","matchedVO",matchedVO);
		}
		return new ModelAndView("loginFailed.member");
	}
	public ModelAndView registForm(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("regist.member");
	}
	public ModelAndView updateForm(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		String destination="update.member";
		if(((MemberVO)session.getAttribute("memberVO")).getId().equals("admin")){
			destination="memberManage.member";
			List list=null;
			try {
				list = memberService.memberGetAllMember();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return new ModelAndView(destination,"memberList",list);
		}
		return new ModelAndView("update.member");
	}
	public ModelAndView cancel(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("index");
	}
	public ModelAndView memberLogout(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		if(session!=null) session.invalidate();
		return new ModelAndView("index");
	}
	public ModelAndView memberUpdate(HttpServletRequest request,HttpServletResponse response,MemberVO memberVO) throws Exception{
		request.getSession(false).setAttribute("memberVO",memberService.memberUpdate(memberVO));
		return new ModelAndView("updated.member");
	}
	public ModelAndView memberDelete(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		if(session!=null) session.invalidate();
		String id=request.getParameter("id");
		System.out.println("세션종료");
		boardService.boardReplyAllDeleteById(id);
		System.out.println("리플삭제");
		boardService.boardDeleteContentsById(id);
		System.out.println("글삭제");
		memberService.memberDelete(id);
		System.out.println("아디삭제");
		return new ModelAndView("deleted.member");
	}
	public ModelAndView membersDelete(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		String[] list=request.getParameterValues("memberCheckbox");
		for(int i=0;i<list.length;i++){
			String id=list[i];
			boardService.boardReplyAllDeleteById(id);
			boardService.boardDeleteContentsById(id);
			memberService.memberDelete(id);
		}
		return new ModelAndView("memberManage.member","memberList",memberService.memberGetAllMember());
	}
	
	// 2012-04-26 안드로이드 팀
	 public ModelAndView appMemberExistId(HttpServletRequest request,
	   HttpServletResponse response) {
	  boolean flag = false;
	  PrintWriter out = null;
	  try {
	   flag = memberService.memberExistId(request.getParameter("id"));
	   out = response.getWriter();
	   out.print(flag);
	   out.close();
	  } catch (SQLException e) {
	   e.printStackTrace();
	  } catch (IOException e) {
	   e.printStackTrace();
	  }
	  return null;
	 }

	 public ModelAndView appMemberRegist(HttpServletRequest request,
	   HttpServletResponse response) throws Exception {
	  String id = request.getParameter("id");
	  String password = request.getParameter("password");
	  String name = request.getParameter("name");
	  MemberVO memberVO = new MemberVO(id, password, name);
	  memberService.memberRegist(memberVO);
	  return null;
	 }

	 public ModelAndView appMemberLogin(HttpServletRequest request,
	   HttpServletResponse response) {
	  String id = request.getParameter("id");
	  String password = request.getParameter("password");
	  MemberVO memberVO = new MemberVO(id, password, null);
	  MemberVO memberVOLogedIn = null;
	  PrintWriter out = null;
	  boolean loginCheck = false;
	  try {
	   memberVOLogedIn = memberService.memberLogin(memberVO);
	   out = response.getWriter();
	  } catch (SQLException e) {
	   e.printStackTrace();
	  } catch (IOException e) {
	   e.printStackTrace();
	  }
	  if (memberVOLogedIn != null) {
	   loginCheck = true;
	  }
	  out.print(loginCheck);
	  return null;
	 }
}
